<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="./vue.js"></script>
    <div id="app">
        <!--3、使用子组件-->
        <App></App>
    </div>

    <script>
        /*
        beforeCreate
        created
        beforemount
        mounted
        beforeUpdate
        updated
        activated  激活
        deactivated  停用
        配合keep-alive
        beforeDestroy
        destroyed
        */
        Vue.component('Test',{
            data(){
                return {
                    msg: '白开水'
                }
            },
            methods: {
                handleClick(){
                    this.msg = 'AAAA'
                }
            },
            template:`
                <div>
                    <button @click='handleClick'>改变你</button>
                    <h3>{{msg}}</h3>
                </div>
            `,
            beforeCreate(){
                console.log('组件创建之前',this.$data);
            },
            created(){
                // 非常重要的事情，在此发送ajax，请求后端的数据
                console.log('组件创建完成',this.$data);
            },
            beforeMount() {
                // 即将挂载
                console.log('DOM挂载之前',document.getElementById('app'));
            },
            mounted(){
                // 发送ajax
                console.log('DOM挂载完成',document.getElementById('app'));
            },
            beforeUpdate() {
                // 获取更新之前的DOM
                console.log('更新之前的DOM', document.getElementById('app').innerHTML);
            },
            updated(){
                // 获取最新的DOM
                console.log('更新之后的DOM', document.getElementById('app').innerHTML);
            },
            beforeDestroy() {
                console.log('销毁之前');
            },
            destroyed() {
                console.log('销毁完成');
            },
            activated(){
                console.log('组件被激活了');
            },
            deactivated() {
                console.log('组件被停用了');
            },
        })


        // 1. 创建组件
        const App = {
            data() {
                return {
                    isShow: true
                }
            },

            components:{
            },
            methods:{
                clickHandler(){
                    this.isShow = !this.isShow;
                }
            },
            template: `
               <div>
                    <keep-alive>
                        <Test v-if='isShow'></Test>
                    </keep-alive>
                    <button @click='clickHandler'>改变组件生死</button>
               </div>
            `,
        }
        new Vue({
            el: '#app',
            data: {

            },
            components:{
                // 2. 挂载子组件
                App
            }

        })
    </script>
</body>
</html>